package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.FavoriteBusinessService;
import com.neusoft.elmboot.util.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/elm/favorites")
public class FavoriteBusinessController {

    @Autowired
    private FavoriteBusinessService favoriteBusinessService;

    @PostMapping("/add")
    public ResultBody addFavorite(@RequestParam String userId, @RequestParam String businessId) {
        try {
            favoriteBusinessService.addFavorite(userId, businessId);
            return ResultBody.success("添加收藏成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("添加收藏失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/remove")
    public ResultBody removeFavorite(@RequestParam String userId, @RequestParam String businessId) {
        try {
            favoriteBusinessService.removeFavorite(userId, businessId);
            return ResultBody.success("取消收藏成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("取消收藏失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/status")
    public ResultBody favoriteStatus(@RequestParam String userId, @RequestParam String businessId) {
        try {
            boolean favorite = favoriteBusinessService.isFavorite(userId, businessId);
            Map<String, Object> result = new HashMap<>();
            result.put("favorite", favorite);
            return ResultBody.success("查询收藏状态成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("查询收藏状态失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/list")
    public ResultBody listFavorites(@RequestParam String userId) {
        try {
            List<Business> favorites = favoriteBusinessService.getUserFavorites(userId);
            return ResultBody.success("获取收藏列表成功", favorites);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取收藏列表失败：" + e.getMessage(), null);
        }
    }
}
